﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DTO;
using BUS;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace Nhom16_PTTKHTTT_12HCB
{
    public partial class frmKhoHang : DevComponents.DotNetBar.Office2007Form
    {
        public delegate void ThemMoi();
        public delegate void CapNhat();

        public string Action = "";
        public string ID = null;

        frmThem_Sua_LoaiNVL frmThemSuaLoaiNVL;
        frmThem_Sua_NVL frmthemnvl;
        frmThem_Sua_NCC frmthemsuancc;

        LaoiNVL_BUS loainvl_bus;
        LoaiNVL loainvl_dto;

        NguyenVatLieu_BUS nvl_bus;
        NguyenVatLieu nvl_dto;

        NCC_BUS ncc_bus;
        NCC ncc_dto;

        PhieuNhap_BUS phieunhap_bus;
        PhieuNhap phieunhap_dto;

        CTPhieuNhap ctpn_dto;
        CTPhieuNhap_BUS ctpn_bus;

        PhieuXuat phieuxuat_dto;
        PhieuXuat_BUS phieuxuat_bus;

        CTPhieuXuat ctpx_dto;
        CTPhieuXuat_BUS ctpx_bus;
        public frmKhoHang()
        {
            InitializeComponent();
        }
        
        private void tabControl1_SelectedTabChanged(object sender, TabStripTabChangedEventArgs e)
        {

        }

        private void frmKhoHang_Load(object sender, EventArgs e)
        {
            comboBoxExLoaiTK.SelectedIndex = 0;
            cbxTKNhap.Enabled = true;
            //load trong phan quan ly nguyen vat lieu, loai nguyen vat lieu.
            Load_DSLoaiNVL();
            LoadComboboxLoaiNVL();
            Load_DSNVL();
            cbxLoaiNVL_PN.SelectedIndex = 1;
            cbxLoaiNVL_PX.SelectedIndex = 1;
            //PHAN PHIEU NHAP
            //load cot trong data gridview
            //phieu nhap
            nvl_bus = new NguyenVatLieu_BUS();
            colTenNVL_PN.DataSource = nvl_bus.DanhSachNVL();
            colTenNVL_PN.DisplayMember = "TenNVL";
            colTenNVL_PN.ValueMember = "MaNVL";

            loainvl_bus = new LaoiNVL_BUS();
            colLoaiNVL_PN.DataSource = loainvl_bus.DanhSachLoaiNVL();
            colLoaiNVL_PN.DisplayMember = "TenLoaiNVL";
            colLoaiNVL_PN.ValueMember = "MaLoaiNVL";

            ncc_bus = new NCC_BUS();
            colNhaCungCap.DataSource = ncc_bus.DanhSachNCC() ;
            colNhaCungCap.DisplayMember = "TenNCC";
            colNhaCungCap.ValueMember = "MaNCC";
            //phieu xuat
            nvl_bus = new NguyenVatLieu_BUS();
            colTenNVL_PX.DataSource = nvl_bus.DanhSachNVL();
            colTenNVL_PX.DisplayMember = "TenNVL";
            colTenNVL_PX.ValueMember = "MaNVL";

            loainvl_bus = new LaoiNVL_BUS();
            colLoaiNVL_PX.DataSource = loainvl_bus.DanhSachLoaiNVL();
            colLoaiNVL_PX.DisplayMember = "TenLoaiNVL";
            colLoaiNVL_PX.ValueMember = "MaLoaiNVL";
            //load cac combobox trong datagridview trong phan quan ly dat loai nguyen vat lieu
            ncc_bus = new NCC_BUS();
            colTenNCC_DatNVL.DataSource = ncc_bus.DanhSachNCC();
            colTenNCC_DatNVL.DisplayMember = "TenNCC";
            colTenNCC_DatNVL.ValueMember = "MaNCC";
            //phieu xuat
            nvl_bus = new NguyenVatLieu_BUS();
            colTenNVL_DatNVL.DataSource = nvl_bus.DanhSachNVL();
            colTenNVL_DatNVL.DisplayMember = "TenNVL";
            colTenNVL_DatNVL.ValueMember = "MaNVL";

            loainvl_bus = new LaoiNVL_BUS();
            colLoaiNVL_DatNVL.DataSource = loainvl_bus.DanhSachLoaiNVL();
            colLoaiNVL_DatNVL.DisplayMember = "TenLoaiNVL";
            colLoaiNVL_DatNVL.ValueMember = "MaLoaiNVL";
            //load danh sach nha cung cap
            LoadDanhSachNCC();
            //load combobox trong phan quan ly dat nguyen vat lieu
            LoadDSLoaiNVL_DatNVL();
            //load combobox trong phan thong ke nhap nguyen vat lieu
            loainvl_bus = new LaoiNVL_BUS();
            cbxTKNhap.DataSource = loainvl_bus.DanhSachLoaiNVL();
            cbxTKNhap.DisplayMember = "TenLoaiNVL";
            cbxTKNhap.ValueMember = "MaLoaiNVL";
        }
        int dong_LoaiNVL;
        private void dtgLoaiNVL_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong_LoaiNVL = e.RowIndex;            
        }
        void LoadComboboxLapDSNCC()
        {
            ncc_bus = new NCC_BUS();
            colTenNCC_DatNVL.DataSource = ncc_bus.DanhSachNCC();
            colTenNCC_DatNVL.DisplayMember = "TenNCC";
            colTenNCC_DatNVL.ValueMember = "MaNCC";
            //phieu xuat
            nvl_bus = new NguyenVatLieu_BUS();
            colTenNVL_DatNVL.DataSource = nvl_bus.DanhSachNVL();
            colTenNVL_DatNVL.DisplayMember = "TenNVL";
            colTenNVL_DatNVL.ValueMember = "MaNVL";

            loainvl_bus = new LaoiNVL_BUS();
            colLoaiNVL_DatNVL.DataSource = loainvl_bus.DanhSachLoaiNVL();
            colLoaiNVL_DatNVL.DisplayMember = "TenLoaiNVL";
            colLoaiNVL_DatNVL.ValueMember = "MaLoaiNVL";
        }
        void LoadComboboxLoaiNVL()
        {
            loainvl_bus = new LaoiNVL_BUS();
            //combobox trong quan ly nguyen vat lieu
            cbxLoaiNVL.DataSource = loainvl_bus.DanhSachLoaiNVL();
            cbxLoaiNVL.DisplayMember = "TenLoaiNVL";
            cbxLoaiNVL.ValueMember = "MaLoaiNVL";
            //combobox trong phieu nhap 
            cbxLoaiNVL_PN.DataSource = loainvl_bus.DanhSachLoaiNVL();
            cbxLoaiNVL_PN.DisplayMember = "TenLoaiNVL";
            cbxLoaiNVL_PN.ValueMember = "MaLoaiNVL";
            //cbxLoaiNVL_PN.SelectedIndex = 0;
            //combobox trong phieu xuat
            cbxLoaiNVL_PX.DataSource = loainvl_bus.DanhSachLoaiNVL();
            cbxLoaiNVL_PX.DisplayMember = "TenLoaiNVL";
            cbxLoaiNVL_PX.ValueMember = "MaLoaiNVL";
            //cbxLoaiNVL_PX.SelectedIndex = 0;
        }
        private void btnLapPhieuNhap_Click(object sender, EventArgs e)
        {
            if (txtSoLuong_PN.Text == "")
            {
                MessageBox.Show("Chưa nhập số lượng", "Thông báo");
                txtSoLuong_PN.Select();
                return;
            }
            if (txtDonGia_PN.Text == "")
            {
                MessageBox.Show("Chưa nhập số lượng", "Thông báo");
                txtSoLuong_PN.Select();
                return;
            }
            bool flag = true;
            //1. luu vao phieu nhap
            phieunhap_bus = new PhieuNhap_BUS();
            phieunhap_dto = new PhieuNhap();
            phieunhap_dto.MaPhieuNhap = phieunhap_bus.MaPNTang();
            phieunhap_dto.MaNV = ThongTinDangNhap.MaNV;
            phieunhap_dto.NgayLap = DateTime.Now;
            flag = phieunhap_bus.ThemPhieuNhap(phieunhap_dto);
            //2. luu vào bang chi tiet phieu nhap
            for (int i = 0; i < dtgLapPhieuNhap.Rows.Count; i++)
            {
                //2.1 cap nhat lai thong tin so luong ton hien tai cua nguyen vat lieu
                nvl_bus = new NguyenVatLieu_BUS();
                nvl_dto = new NguyenVatLieu();
                nvl_dto.MaNuyenVatLieu = dtgLapPhieuNhap.Rows[i].Cells["colTenNVL_PN"].Value.ToString();
                System.Data.DataTable nvl = nvl_bus.DanhSachNVL_Ma(nvl_dto.MaNuyenVatLieu);
                nvl_dto.MaLoaiNVL = nvl.Rows[0]["MaLoaiNVL"].ToString();
                nvl_dto.TenNVl = nvl.Rows[0]["TenNVL"].ToString();
                int temp = int.Parse(nvl.Rows[0]["SoLuongTon"].ToString()) + int.Parse(txtSoLuong_PN.Text);
                nvl_dto.SoLuongTon = temp;
                nvl_dto.DonViTinh = nvl.Rows[0]["DVT"].ToString();
                flag = nvl_bus.CapNhatNVL(nvl_dto);
                //2.2 them vao bang chi tiet phieu nhap
                ctpn_bus = new CTPhieuNhap_BUS();
                ctpn_dto = new CTPhieuNhap();
                ctpn_dto.MaPhieuNhap = phieunhap_dto.MaPhieuNhap;
                ctpn_dto.MaNVL = dtgLapPhieuNhap.Rows[i].Cells["colTenNVL_PN"].Value.ToString();
                ctpn_dto.MaNCC = dtgLapPhieuNhap.Rows[i].Cells["colNhaCungCap"].Value.ToString();
                ctpn_dto.DonGiaNhap = decimal.Parse(txtDonGia_PN.Text);
                ctpn_dto.SoLuong = int.Parse(txtSoLuong_PN.Text);
                ctpn_dto.SoLuongTonHienTai = nvl_dto.SoLuongTon;

                flag = ctpn_bus.ThemCTPhieuNhap(ctpn_dto);
            }
            //3. cap nhat lai nguyen vat lieu
            if (flag == true)
            {
                MessageBox.Show("Lập thành công", "Thông báo");
                frm_KhiNVLThayDoi();                
            }                
            if (MessageBoxEx.Show("Bạn có muốn xuất ra file excel không?", "Xuất file", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                //ExportToExcel ex = new ExportToExcel();
                //ex.Export(dtgLapPhieuNhap, "Phiếu nhập");
            }
        }
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmThemNCC_NVL frm = new frmThemNCC_NVL();
            frm.ShowDialog();
        }
        private void cbxTenNVL_PN_SelectedIndexChanged(object sender, EventArgs e)
        {
            ncc_bus = new NCC_BUS();
            cbxNCC_PN.DataSource = ncc_bus.DanhSachNCC_MaNVL(cbxTenNVL_PN.SelectedValue.ToString());
            cbxNCC_PN.DisplayMember = "TenNCC";
            cbxNCC_PN.ValueMember = "MaNCC";
        }
        private void cbxLoaiNVL_PX_SelectedIndexChanged(object sender, EventArgs e)
        {
            nvl_bus = new NguyenVatLieu_BUS();
            cbxTenNVL_PX.DataSource = nvl_bus.DanhSachNVL_MaLoaiNVL(cbxLoaiNVL_PX.SelectedValue.ToString());
            cbxTenNVL_PX.DisplayMember = "TenNVL";
            cbxTenNVL_PX.ValueMember = "MaNVL";
        }
        private void btnThoat_NVL_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #region "QUAN LY LOAI NGUYEN VAT LIEU"
        //THEM LOAI NGUYEN VAT LIEU
        private void btnThem_Click(object sender, EventArgs e)
        {
            frmThemSuaLoaiNVL = new frmThem_Sua_LoaiNVL();
            frmThemSuaLoaiNVL.KhiThemMoi += frm_KhiLoaiNVLThayDoi;
            frmThemSuaLoaiNVL.Action = "Insert";
            frmThemSuaLoaiNVL.ShowDialog();
        }

        //CAP NHAT LOAI NGUYEN VAT LIEU
        private void btnSua_Click(object sender, EventArgs e)
        {
            frmThemSuaLoaiNVL = new frmThem_Sua_LoaiNVL();
            frmThemSuaLoaiNVL.KhiCapNhat += frm_KhiLoaiNVLThayDoi;
            frmThemSuaLoaiNVL.Action = "Update";
            frmThemSuaLoaiNVL.ID = dtgLoaiNVL.Rows[dong_LoaiNVL].Cells["MaLoaiNVL"].Value.ToString();
            frmThemSuaLoaiNVL.ShowDialog();
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            loainvl_bus = new LaoiNVL_BUS();
            loainvl_dto = new LoaiNVL();
            loainvl_dto.MaLoaiNVL = dtgLoaiNVL.Rows[dong_LoaiNVL].Cells["MaLoaiNVL"].Value.ToString();
            if (loainvl_bus.XoaLoaiNVL(loainvl_dto) == true)
            {
                MessageBox.Show("Xóa thành công", "Thông báo");
                frm_KhiLoaiNVLThayDoi();
            }
            else
                MessageBox.Show("Xóa thất bại", "Thông báo");
        }
        private void dtgLoaiNVL_DoubleClick(object sender, EventArgs e)
        {
            btnSua_Click(sender, e);
        }

        void frm_KhiLoaiNVLThayDoi()
        {
            Load_DSLoaiNVL();
            LoadComboboxLoaiNVL();
        }

        void Load_DSLoaiNVL()
        {
            loainvl_bus = new LaoiNVL_BUS();
            dtgLoaiNVL.AutoGenerateColumns = false;
            dtgLoaiNVL.DataSource = loainvl_bus.DanhSachLoaiNVL();
        }

        void Load_DSNVL()
        {
            nvl_bus = new NguyenVatLieu_BUS();
            dtgNVL.AutoGenerateColumns = false;
            dtgNVL.DataSource = nvl_bus.DanhSachNVL();
        }
        #endregion
        #region"QUAN LY NGUYEN VAT LIEU"
        //----------------------------------    NGUYEN VAT LIEU------------------------------------
        int dong_NVL;
        private void dtgNVL_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong_NVL = e.RowIndex;
            nvl_bus = new NguyenVatLieu_BUS();
            System.Data.DataTable nvl = new System.Data.DataTable();

            nvl = nvl_bus.DanhSachNVL_Ma(dtgNVL.Rows[dong_NVL].Cells["MaNVL"].Value.ToString());

            txtMaNVL.Text = nvl.Rows[0]["MaNVL"].ToString();
            txtTenNVL.Text = nvl.Rows[0]["TenNVL"].ToString();
            txtSoLuongTon.Text = nvl.Rows[0]["SoLuongTon"].ToString();
            cbxLoaiNVL.SelectedValue = nvl.Rows[0]["MaLoaiNVL"].ToString();
        }
        void frm_KhiNVLThayDoi()
        {
            Load_DSNVL();
        }
        private void btnThem_NVL_Click(object sender, EventArgs e)
        {
            frmthemnvl = new frmThem_Sua_NVL();
            frmthemnvl.KhiThemMoiNVL += frm_KhiNVLThayDoi;
            frmthemnvl.Action = "Insert";
            frmthemnvl.ShowDialog();
        }

        private void btnXoa_NVL_Click(object sender, EventArgs e)
        {
            nvl_bus = new NguyenVatLieu_BUS();
            nvl_dto = new NguyenVatLieu();
            nvl_dto.MaNuyenVatLieu = dtgNVL.Rows[dong_NVL].Cells["MaNVL"].Value.ToString();
            if (nvl_bus.XoaNVL(nvl_dto) == true)
            {
                MessageBox.Show("Xóa thành công", "Thông báo");
                frm_KhiNVLThayDoi();
            }
            else
                MessageBox.Show("Xóa thát bại","Thông báo");
        }

        private void btnSua_NVL_Click(object sender, EventArgs e)
        {
            frmthemnvl = new frmThem_Sua_NVL();
            frmthemnvl.KhiCapNhatNVL += frm_KhiNVLThayDoi;
            frmthemnvl.Action = "Update";
            frmthemnvl.ID = dtgNVL.Rows[dong_NVL].Cells["MaNVL"].Value.ToString();
            frmthemnvl.ShowDialog();
        }
        
        private void dtgNVL_DoubleClick(object sender, EventArgs e)
        {
            btnSua_NVL_Click(sender, e);
        }
        #endregion
        #region"QUAN LY PHIEU NHAP"
        private void cbxLoaiNVL_PN_SelectedIndexChanged(object sender, EventArgs e)
        {
            nvl_bus = new NguyenVatLieu_BUS();
            cbxTenNVL_PN.DataSource = nvl_bus.DanhSachNVL_MaLoaiNVL(cbxLoaiNVL_PN.SelectedValue.ToString());
            cbxTenNVL_PN.DisplayMember = "TenNVL";
            cbxTenNVL_PN.ValueMember = "MaNVL";
        }

        private void txtSoLuong_PN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true; 
        }
        private void txtDonGia_PN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true; 
        }
        private void txtSoLuong_PX_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true; 
        }
        //themthong tin nguyen vat lieu vao trong gridview
        private void txtThem_PN_Click(object sender, EventArgs e)
        {
            if (txtSoLuong_PN.Text == "")
            {
                MessageBox.Show("Chưa nhập số lượng","Thông báo");
                txtSoLuong_PX.Select();
                return;
            }
            if (txtDonGia_PN.Text == "")
            {
                MessageBox.Show("Chưa nhập đơn giá", "Thông báo");
                txtDonGia_PN.Select();
                return;
            }
            ////truong hop co roi lai them nua thi cap nhat lai dong đó.
            int flag = -1;//ko trung
            for (int i = 0; i < dtgLapPhieuNhap.Rows.Count; i++)
            {
                if (dtgLapPhieuNhap.Rows[i].Cells["colTenNVL_PN"].Value == cbxTenNVL_PN.SelectedValue
                  && dtgLapPhieuNhap.Rows[i].Cells["colNhaCungCap"].Value == cbxNCC_PN.SelectedValue)
                    flag = i;//trung thi cap nhat lai so luong va gia
            }
            if (flag != -1)//da trung thi cap nhat lai so luong va gia
            {
                int i_SoLuong = int.Parse(dtgLapPhieuNhap.Rows[flag].Cells["SoLuong_PN"].Value.ToString());
                dtgLapPhieuNhap.Rows[flag].Cells["SoLuong_PN"].Value = i_SoLuong + int.Parse(txtSoLuong_PN.Text);
            }
            else
            {
                //thong tin nguyen vat lieu          
                nvl_bus = new NguyenVatLieu_BUS();
                System.Data.DataTable nvl = nvl_bus.DanhSachNVL_Ma(cbxTenNVL_PN.SelectedValue.ToString());
                string dvt = nvl.Rows[0]["DVT"].ToString();
                dtgLapPhieuNhap.Rows.Add(cbxTenNVL_PN.SelectedValue, cbxNCC_PN.SelectedValue, cbxLoaiNVL_PN.SelectedValue, txtSoLuong_PN.Text, txtDonGia_PN.Text, dvt);
            }
        }
        private void btnXoa_PN_Click(object sender, EventArgs e)
        {
            dtgLapPhieuNhap.Rows.RemoveAt(dong_dtgLapPhieuNhap);
        }
        int dong_dtgLapPhieuNhap;
        private void dtgLapPhieuNhap_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong_dtgLapPhieuNhap = e.RowIndex;
            
        }
        private void btnPhieuNhapMoi_Click(object sender, EventArgs e)
        {
            dtgLapPhieuNhap.Rows.Clear();
            
        }
        private void LuuExcel(string strTieude)
        {
            
        }
        #endregion
        #region"QUAN LY NHA CUNG CAP"
        private void btnThem_NCC_Click(object sender, EventArgs e)
        {
            frmthemsuancc = new frmThem_Sua_NCC();
            frmthemsuancc.KhiThemMoiNCC += frm_KhiNCCThayDoi;
            frmthemsuancc.Action = "Insert";
            frmthemsuancc.ShowDialog();
        }

        private void btnXoa_NCC_Click(object sender, EventArgs e)
        {
            ncc_dto = new NCC();
            ncc_bus = new NCC_BUS();
            ncc_dto.MaNCC = dtgNCC.Rows[dong_ncc].Cells["MaNCC"].Value.ToString();
            if (ncc_bus.XoaNCC(ncc_dto) == true)
            {
                MessageBox.Show("Xóa thành công", "Thông báo");
                frm_KhiNCCThayDoi();
            }
            else
                MessageBox.Show("Xóa thất bại","Thông báo");
        }

        private void bntSua_NCC_Click(object sender, EventArgs e)//ncc
        {
            frmthemsuancc = new frmThem_Sua_NCC();
            frmthemsuancc.KhiCapNhatNCC += frm_KhiNCCThayDoi;
            frmthemsuancc.Action = "Update";
            frmthemsuancc.ID = dtgNCC.Rows[dong_ncc].Cells["MaNCC"].Value.ToString();
            frmthemsuancc.ShowDialog();
        }
        void frm_KhiNCCThayDoi()
        {
            LoadDanhSachNCC();
        }
        void LoadDanhSachNCC()
        {
            ncc_bus = new NCC_BUS();
            dtgNCC.AutoGenerateColumns = false;
            dtgNCC.DataSource = ncc_bus.DanhSachNCC();
        }
        int dong_ncc;
        private void dtgNCC_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong_ncc = e.RowIndex;
            
        }
        #endregion              

        private void btnThem_PX_Click(object sender, EventArgs e)
        {
            if (txtSoLuong_PX.Text == "")
            {
                MessageBox.Show("Chưa nhập số lượng","Thông báo");
                txtSoLuong_PX.Select();
                return;
            }
            //
            //kiem tra so luong xuat ra.
            nvl_bus = new NguyenVatLieu_BUS();
            nvl_dto = new NguyenVatLieu();
            System.Data.DataTable dt = nvl_bus.DanhSachNVL_Ma(cbxTenNVL_PX.SelectedValue.ToString());
            nvl_dto.DonViTinh = dt.Rows[0]["DVT"].ToString();
            if (int.Parse(txtSoLuong_PX.Text) > int.Parse(dt.Rows[0]["SoLuongTon"].ToString()))
            {
                MessageBox.Show("Số lượng xuất không được lớn hơn số lượng hiện tại", "Thông báo");
                txtSoLuong_PX.Select();
                return;
            }
            //
            int flag = -1;//ko trung
            for (int i = 0; i < dtgLapPhieuXuat.Rows.Count; i++)
            {
                if (dtgLapPhieuXuat.Rows[i].Cells["colTenNVL_PX"].Value == cbxTenNVL_PX.SelectedValue
                  && dtgLapPhieuXuat.Rows[i].Cells["colLoaiNVL_PX"].Value == cbxLoaiNVL_PX.SelectedValue)
                    flag = i;//trung thi cap nhat lai so luong va gia
            }
            if (flag != -1)//da trung thi cap nhat lai so luong va gia
            {
                int i_SoLuong = int.Parse(dtgLapPhieuXuat.Rows[flag].Cells["SoLuong_PX"].Value.ToString());
                dtgLapPhieuXuat.Rows[flag].Cells["SoLuong_PX"].Value = i_SoLuong + int.Parse(txtSoLuong_PX.Text);
            }
            else
                dtgLapPhieuXuat.Rows.Add(cbxTenNVL_PX.SelectedValue, cbxLoaiNVL_PX.SelectedValue, txtSoLuong_PX.Text, nvl_dto.DonViTinh);
        }
        private void btnXoa_PX_Click(object sender, EventArgs e)
        {
            dtgLapPhieuXuat.Rows.RemoveAt(dong_dtgLapPhieuXuat);
        }
        int dong_dtgLapPhieuXuat;
        private void dtgLapPhieuXuat_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong_dtgLapPhieuXuat = e.RowIndex;            
        }
        private void btnPhieuXuatMoi_Click(object sender, EventArgs e)
        {
            dtgLapPhieuXuat.Rows.Clear();
        }
        private void btnThoat_PX_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void txtSoLuong_PX_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true; 
        }
        private void btnLapPhieuXuat_PX_Click(object sender, EventArgs e)
        {
            bool flag = true;
            //1. them vao phieu xuat.
            phieuxuat_bus = new PhieuXuat_BUS();
            phieuxuat_dto = new PhieuXuat();
            phieuxuat_dto.MaPX = phieuxuat_bus.MaPXTang();
            phieuxuat_dto.NgayLap = DateTime.Now;
            phieuxuat_dto.MaNV = ThongTinDangNhap.MaNV;
            flag = phieuxuat_bus.ThemPhieuXuat(phieuxuat_dto);
            //2.
            for (int i = 0; i < dtgLapPhieuXuat.Rows.Count; i++)
            {
                //2.1 cap nhat lai thong tin so luong ton hien tai cua nguyen vat lieu
                nvl_bus = new NguyenVatLieu_BUS();
                nvl_dto = new NguyenVatLieu();
                nvl_dto.MaNuyenVatLieu = dtgLapPhieuXuat.Rows[i].Cells["colTenNVL_PX"].Value.ToString();
                System.Data.DataTable nvl = nvl_bus.DanhSachNVL_Ma(nvl_dto.MaNuyenVatLieu);
                nvl_dto.MaLoaiNVL = nvl.Rows[0]["MaLoaiNVL"].ToString();
                nvl_dto.TenNVl = nvl.Rows[0]["TenNVL"].ToString();
                int temp = int.Parse(nvl.Rows[0]["SoLuongTon"].ToString()) - int.Parse(txtSoLuong_PX.Text);
                nvl_dto.SoLuongTon = temp;
                nvl_dto.DonViTinh = nvl.Rows[0]["DVT"].ToString();
                flag = nvl_bus.CapNhatNVL(nvl_dto);
                //2.2 them vao bang chi tiet phieu xuat
                ctpx_bus = new CTPhieuXuat_BUS();
                ctpx_dto = new CTPhieuXuat();
                ctpx_dto.MaPX = phieuxuat_dto.MaPX;
                ctpx_dto.MaNVL = nvl_dto.MaNuyenVatLieu;
                ctpx_dto.SoLuong = int.Parse(txtSoLuong_PX.Text);
                ctpx_dto.SoLuongTonHienTai = nvl_dto.SoLuongTon;
                flag = ctpx_bus.ThemCTPhieuXuat(ctpx_dto);
            }
            if (flag == true)
            {
                MessageBox.Show("Lập phiếu xuất thành công", "Thông báo");
                frm_KhiNVLThayDoi();                
            }
            
            if (MessageBoxEx.Show("Bạn có muốn xuất ra file excel không?", "Xuất file", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                //ExportToExcel ex = new ExportToExcel();
                //ex.Export(dtgLapPhieuXuat, "Phiếu xuất");
            }
        }

        private void txtSoLuong_DatNVL_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true; 
        }

        private void btnThem_DatNVL_Click(object sender, EventArgs e)
        {
            if (txtSoLuong_DatNVL.Text == "")
            {
                MessageBox.Show("Chưa nhập số lượng","Thông báo");
                txtSoLuong_DatNVL.Select();
                return;
            }
            //
            int flag = -1;//ko trung
            for (int i = 0; i < dtgDSDatNVL.Rows.Count; i++)
            {
                if (dtgDSDatNVL.Rows[i].Cells["colLoaiNVL_DatNVL"].Value == cbxLoaiNVL_DatNVL.SelectedValue
                  && dtgDSDatNVL.Rows[i].Cells["colTenNVL_DatNVL"].Value == cbxTenNVL_DatNVL.SelectedValue
                  && dtgDSDatNVL.Rows[i].Cells["colTenNCC_DatNVL"].Value == cbxTenNCC_DatNVL.SelectedValue)
                    flag = i;//trung thi cap nhat lai so luong va gia
             }
            if (flag != -1)//da trung thi cap nhat lai so luong va gia
            {
                int i_SoLuong = int.Parse(dtgDSDatNVL.Rows[flag].Cells["SoLuong_DatNVL"].Value.ToString());
                dtgDSDatNVL.Rows[flag].Cells["SoLuong_DatNVL"].Value = i_SoLuong + int.Parse(txtSoLuong_DatNVL.Text);
            }
            else
                dtgDSDatNVL.Rows.Add(cbxTenNVL_DatNVL.SelectedValue.ToString(),cbxLoaiNVL_DatNVL.SelectedValue.ToString(),cbxTenNCC_DatNVL.SelectedValue.ToString(), txtSoLuong_DatNVL.Text, txtDVT_DatNVL.Text);
            //
        }
        void LoadDSLoaiNVL_DatNVL()
        {
            loainvl_bus = new LaoiNVL_BUS();
            cbxLoaiNVL_DatNVL.DataSource = loainvl_bus.DanhSachLoaiNVL();
            cbxLoaiNVL_DatNVL.DisplayMember = "TenLoaiNVL";
            cbxLoaiNVL_DatNVL.ValueMember = "MaLoaiNVL";
        }

        private void cbxLoaiNVL_DatNVL_SelectedIndexChanged(object sender, EventArgs e)
        {
            nvl_bus = new NguyenVatLieu_BUS();
            cbxTenNVL_DatNVL.DataSource = nvl_bus.DanhSachNVL_MaLoaiNVL(cbxLoaiNVL_DatNVL.SelectedValue.ToString());
            cbxTenNVL_DatNVL.DisplayMember = "TenNVL";
            cbxTenNVL_DatNVL.ValueMember = "MaNVL";
        }

        private void cbxTenLoaiNVL_DatNVL_SelectedIndexChanged(object sender, EventArgs e)
        {
            ncc_bus = new NCC_BUS();
            cbxTenNCC_DatNVL.DataSource = ncc_bus.DanhSachNCC_MaNVL(cbxTenNVL_DatNVL.SelectedValue.ToString());
            cbxTenNCC_DatNVL.DisplayMember = "TenNCC";
            cbxTenNCC_DatNVL.ValueMember = "MaNCC";
            //lay thuoc tinh don vi tinh
            nvl_bus = new NguyenVatLieu_BUS();
            System.Data.DataTable dt = nvl_bus.DanhSachNVL_Ma(cbxTenNVL_DatNVL.SelectedValue.ToString());
            txtDVT_DatNVL.Text = dt.Rows[0]["DVT"].ToString();
        }

        private void btnThoatLapDS_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPhieuMoi_Click(object sender, EventArgs e)
        {
            dtgDSDatNVL.Rows.Clear();
        }

        private void btnXuatPhieuDat_Click(object sender, EventArgs e)
        {
            ExportToExcel ex = new ExportToExcel();
            ex.Export(dtgDSDatNVL, "Danh sách nguyên vật liệu cần đặt");
        }
        #region"THONG KE PHIEU NHAP VA XUAT NGUEYN VAT LIEU"
        private void btnThoat_TKNhap_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
        private void btnHienThi_TKNhap_Click(object sender, EventArgs e)
        {
            
            TimeSpan ts = datTKNhapNgayKT.Value - datTKNhapNgayBD.Value;
            if (ts.Days <= 0)
            {
                MessageBoxEx.Show("Ngày kết thúc phải lớn hơn ngày bắt đầu","Thông báo");
                datTKNhapNgayKT.Select();
                return;
            }
            DateTime bd = new DateTime(datTKNhapNgayBD.Value.Year, datTKNhapNgayBD.Value.Month, datTKNhapNgayBD.Value.Day, 0, 0, 0);
            DateTime kt = new DateTime(datTKNhapNgayKT.Value.Year, datTKNhapNgayKT.Value.Month, datTKNhapNgayKT.Value.Day, 0, 0, 0);
            ReportDocument cryRpt = new ReportDocument();
            if (comboBoxExLoaiTK.SelectedIndex == 0)
            {
                string str = @"..\\..\\Report\\TK_TinhHinhNhapNVL.rpt";
                cryRpt.Load(str);                              
            }
            if (comboBoxExLoaiTK.SelectedIndex == 1)
            {
                string str = @"..\\..\\Report\\TK_TinhHinhXuatNVL.rpt";
                cryRpt.Load(str);
            }
            string MaNVL = "";  
            if (checkBoxXNVL_Nhap.Checked == true)
                MaNVL = cbxTKNhap.SelectedValue.ToString();
            if (checkBoxXNVL_Nhap.Checked == false)
                MaNVL = "null";
            ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            //truyen tham so loai nguyen vat lieu
            crParameterDiscreteValue.Value = MaNVL;
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["@MaLoaiNVL"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            //truyen tham so ngay bat dau
            crParameterDiscreteValue.Value = bd;
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["@NgayBD"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            ////truyen tham so ngay ket thuc
            crParameterDiscreteValue.Value = kt;
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["@NgayKT"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            //load report
            crystalReportViewerTKNhapNVL.ReportSource = cryRpt;
            crystalReportViewerTKNhapNVL.Refresh(); 
        }
        

        private void checkBoxXNVL_Nhap_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxXNVL_Nhap.Checked == true)
            {
                cbxTKNhap.Enabled = true;
            }
            else
            {
                cbxTKNhap.Enabled = false;
            }
        }
        #endregion
    }
}
